package com.loto.insurance.framework.Air.PICC;

import java.util.Calendar;

import org.apache.commons.codec.binary.Base64;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import com.loto.insurance.framework.Air.InsAir;
import com.loto.insurance.framework.Air.InsReturnMsg;
import com.loto.insurance.framework.Air.OrderItemInfo;
import com.loto.insurance.framework.config.ConfigManagementAir;
import com.loto.insurance.framework.config.PICCConfigAir;
import com.loto.utils.Des3;
import com.loto.utils.DateUtils;
import com.loto.utils.Http;

public class PICCInsAir implements InsAir {
  static Logger logger = LogManager.getLogger(PICCInsAir.class);

  static String key = "A1F9879DESJEIWBCJOTY08DYQWF10H5Y";

  private String productCode;

  private PICCConfigAir piccConfigAir;

  public PICCInsAir(String productCode) {
	this.productCode = productCode;
	this.piccConfigAir = getPICCConfigAir();
  }

  @Override
  public InsReturnMsg PolicyApproval(OrderItemInfo info) {
	InsReturnMsg message = new InsReturnMsg();
	Calendar now = Calendar.getInstance();
	Calendar fromPeriod = now;
	fromPeriod.add(Calendar.MINUTE, 20);

	String postDocument = "<?xml version='1.0' encoding='GBK'?>"
	    + "<TXLife>"
	    + "<TXLifeRequest>"
	    + "<TransRefGUID>##Id##</TransRefGUID>"
	    + "<!-- 交易流水号(必填)-->"
	    + "<TransType tc='15'>##TransType##</TransType>"
	    + "<!-- 交易代码(固定) -->"
	    + "<TransExeDate>##Date##</TransExeDate>"
	    + "<!-- 系统当前日期(必填) -->"
	    + "<TransExeTime>##Time##</TransExeTime>"
	    + "<!-- 系统当前时间(必填) -->"
	    + "<OLifE>"
	    + "<Holding id='Holding_1'>"
	    + "<Policy>"
	    + "<ProductCode>##ProductCode##</ProductCode>"
	    + "<!-- 险种代码 (必填)-->"
	    + "<PlanName>##ProductName##</PlanName>"
	    + "<!-- 险种名称，(可选) -->"
	    + "<EffDate>##EffDate##</EffDate>"
	    + "<!--生效日期(必填)-->"
	    + "<FlightNumber>##FlightNumber##</FlightNumber>"
	    + "<!--机票票号(必填)-->"
	    + "</Policy>"
	    + "</Holding>"
	    + "<Relation OriginatingObjectID='Holding_1' RelatedObjectID='Party_1' id='Relation_1'>"
	    + "<OriginatingObjectType tc='4'>Holding</OriginatingObjectType>"
	    + "<RelatedObjectType tc='6'>Party</RelatedObjectType>"
	    + "<RelationRoleCode tc='8'>Owner</RelationRoleCode>"
	    + "<!-- 投保人关系 (必填)-->"
	    + "</Relation>"
	    + "<Relation OriginatingObjectID='Holding_1' RelatedObjectID='Party_2' id='Relation_2'>"
	    + "<OriginatingObjectType tc='4'>Holding</OriginatingObjectType>"
	    + "<RelatedObjectType tc='6'>Party</RelatedObjectType>"
	    + "<RelationRoleCode tc='32'>Insured</RelationRoleCode>"
	    + "<!-- 被保人关系(必填) -->"
	    + "</Relation>"
	    + "<Relation OriginatingObjectID='Party_1' RelatedObjectID='Party_2' id='Relation_4'>"
	    + "<OriginatingObjectType tc='6'>Party</OriginatingObjectType>"
	    + "<RelatedObjectType tc='6'>Party</RelatedObjectType>"
	    + "<RelationRoleCode tc='##RelationRoleCode##'>##Supose##</RelationRoleCode>"
	    + "<!-- 投保人与被保人关系 (必填)-->"
	    + "</Relation>"
	    + "<FormInstance>"
	    + "<id>Form_1</id>"
	    + "<!--固定-->"
	    + "<FormName>1</FormName>"
	    + "<!--固定-->"
	    + "<DocumentControlNumber></DocumentControlNumber>"
	    + "<!--单证号（可选）-->"
	    + "</FormInstance>"
	    + "<Party id='Party_1'>"
	    + "<FullName>##RelationName##</FullName>"
	    + "<!-- 投保人姓名 -->"
	    + "<GovtID>##RelationCertificateNo##</GovtID>"
	    + "<!-- 投保人证件号码 -->"
	    + "<GovtIDTC tc='##RelationCertificateType##'>##RelationCertificateType##</GovtIDTC>"
	    + "<!-- 投保人证件类型 -->" + "<Person>"
	    + "<Gender tc='##RelationSex##'>##RelationSex##</Gender>"
	    + "<!-- 投保人性别 -->" + "<BirthDate>##RelationBirthday##</BirthDate>"
	    + "<!-- 投保人出生日期 -->" + "</Person>" + "<Address id='Address_1'>"
	    + "<Line1>##Address##</Line1>" + "<!-- 投保人邮寄地址 -->"
	    + "<Zip>##ZipCode##</Zip>" + "<!-- 投保人邮寄邮编 -->" + "</Address>"
	    + "<Phone id='Phone_2'>" + "<DialNumber>##Phone##</DialNumber>"
	    + "<!-- 投保人住宅电话 -->" + "<Email>##Email##</Email>" + "<!-- 投保人邮箱 -->"
	    + "</Phone>" + "</Party>" + "<Party id='Party_2'>"
	    + "<FullName>##PersonName##</FullName>" + "<!-- 被保人姓名 -->"
	    + "<GovtID>##CertificateNo##</GovtID>" + "<!-- 被保人证件号码 -->"
	    + "<GovtIDTC tc='##CertificateType##'>##CertificateType##</GovtIDTC>"
	    + "<!-- 被保人证件类型 -->" + "<Person>"
	    + "<Gender tc='##Sex##'>##Sex##</Gender>" + "<!-- 被保人性别 -->"
	    + "<BirthDate>##Birthday##</BirthDate>" + "<!-- 被保人出生日期 -->"
	    + "</Person>" + "<Address id='Address_1'>"
	    + "<Line1>##Address##</Line1>" + "<!-- 被保人邮寄地址 -->"
	    + "<Zip>##ZipCode##</Zip>" + "<!-- 被保人邮寄邮编 -->" + "</Address>"
	    + "<Phone id='Phone_2'>" + "<DialNumber>##Phone##</DialNumber>"
	    + "<!-- 被保人住宅电话 -->" + "<Email>##Email##</Email>" + "<!-- 被保人邮箱 -->"
	    + "</Phone>" + "</Party>" + "</OLifE>"
	    + "<OLifEExtension VendorCode='1'>"
	    + "<CarrierCode>##CarrierCode##</CarrierCode>"
	    + "<!-- 保险公司代码 (固定)<Branch>测试用的JD1940085</Branch>-->"
	    + "<Branch>##Branch##</Branch>" + "<!-- 网点代码 (必填)-->"
	    + "<BankCode>##BankCode##</BankCode>" + "<!-- 交易伙伴代码(固定) -->"
	    + "</OLifEExtension>" + "</TXLifeRequest>" + "</TXLife>";
	postDocument = postDocument.replace("##ProductCode##",
	    piccConfigAir.getProductCode());
	postDocument = postDocument.replace("##TransType##",
	    piccConfigAir.getApprovalTransType());
	postDocument = postDocument.replace("##ProductName##",
	    piccConfigAir.getProductName());
	postDocument = postDocument.replace("##FlightNumber##",
	    piccConfigAir.getFlightNumber());
	postDocument = postDocument.replace("##CarrierCode##",
	    piccConfigAir.getCarrierCode());
	postDocument = postDocument
	    .replace("##Branch##", piccConfigAir.getBranch());
	postDocument = postDocument.replace("##BankCode##",
	    piccConfigAir.getBankCode());

	postDocument = postDocument.replace("##Id##", info.getTransOrderNo());
	postDocument = postDocument.replace("##Date##",
	    DateUtils.dateToString(now.getTime(), "yyyy-MM-dd"));
	postDocument = postDocument.replace("##Time##",
	    DateUtils.dateToString(now.getTime(), "hh:mm:ss"));
	postDocument = postDocument.replace("##EffDate##",
	    DateUtils.dateToString(info.getFromPeriod(), "yyyy-MM-dd"));

	postDocument = postDocument
	    .replace("##PersonName##", info.getInsuredName());
	postDocument = postDocument.replace("##CertificateNo##",
	    info.getInsuredCredentialNo());
	postDocument = postDocument.replace("##CertificateType##",
	    getCertificateType(info.getInsuredCredentialType()));

	postDocument = postDocument.replace("##RelationName##",
	    info.getHolderName());
	postDocument = postDocument.replace("##RelationCertificateNo##",
	    info.getInsuredCredentialNo());
	postDocument = postDocument.replace("##RelationCertificateType##",
	    getCertificateType(info.getInsuredCredentialType()));

	postDocument = postDocument.replace("##Birthday##",
	    DateUtils.dateToString(info.getInsuredBirth(), "yyyy-MM-dd"));
	postDocument = postDocument.replace("##Sex##",
	    getGender(info.getInsuredGender()));

	postDocument = postDocument.replace("##RelationBirthday##",
	    DateUtils.dateToString(info.getHolderBirth(), "yyyy-MM-dd"));
	postDocument = postDocument.replace("##RelationSex##",
	    getGender(info.getHolderGender()));

	if (info.getHolderName() == info.getInsuredName()) {
	  postDocument = postDocument.replace("##RelationRoleCode##", "5");
	  postDocument = postDocument.replace("##Supose##", "Supose");
	} else {
	  postDocument = postDocument.replace("##RelationRoleCode##", "4");
	  postDocument = postDocument.replace("##Supose##", "Family");

	}

	postDocument = postDocument.replace("##Address##", "");
	postDocument = postDocument.replace("##ZipCode##", "");
	postDocument = postDocument.replace("##Phone##", info.getInsuredTel());
	postDocument = postDocument.replace("##Email##", "");

	logger.info(postDocument);

	String encry = Des3.des3EncodeECB(key, postDocument);
	String response = Http.doPost(piccConfigAir.getApprovalPostUrl(), encry,
	    "UTF-8");
	String responseXml = Des3.ees3DecodeECB(key, response);
	logger.info(responseXml);

	return message;
  }

  @Override
  public InsReturnMsg PolicyQuery(OrderItemInfo info) {
	// TODO Auto-generated method stub
	return null;
  }

  @Override
  public InsReturnMsg PolicyCancel(OrderItemInfo info) {
	// TODO Auto-generated method stub
	return null;
  }

  public String getProductCode() {
	return productCode;
  }

  public void setProductCode(String productCode) {
	this.productCode = productCode;
  }

  public PICCConfigAir getPICCConfigAir() {
	ConfigManagementAir config = new ConfigManagementAir(productCode);
	return config.getPICCConfiguration();
  }

  private String getGender(String gender) {
	String strGender = "1";
	if (gender == "女") {
	  strGender = "2";
	}
	return strGender;
  }

  private String getCertificateType(String certificateType) {
	if (certificateType == "身份证") {
	  return "1";
	} else if (certificateType == "军人证") {
	  return "2";
	} else if (certificateType == "护照") {
	  return "3";
	} else if (certificateType == "出生证") {
	  return "4";
	} else if (certificateType == "异常身份证") {
	  return "5";
	} else if (certificateType == "回乡证") {
	  return "6";
	} else if (certificateType == "户口本") {
	  return "7";
	} else if (certificateType == "警官证") {
	  return "8";
	} else {
	  return "9";
	}
  }
}
